
mb.string = {
  escapeHTML : function(str) {
    return str ? 
           str.replace(/&/g, '&amp;')
              .replace(/</g, '&lt;')
              .replace(/>/g, '&gt;')
              .replace(/"/g, '&quot;') :
           ''; 
  }
};

mb.url = {
  urlEncode: function(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length)
    {
      var match = regex.exec(clearString.substr(x));
      if (match != null && match.length > 1 && match[1] != '')
      {
        output += match[1];
        x += match[1].length;
      }
      else
      {
        if (clearString[x] == ' ')
          output += '+';
        else
        {
          var charCode = clearString.charCodeAt(x);
          var hexVal = charCode.toString(16);
          output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
        }
        x++;
      }
    }
    return output;
  }
  ,
  getUrlParameters: function (url) {
    if(!url)
      url = window.location.href;
    var urlParam = url.split('?')[1];
    if(urlParam != null) {
      var paramsArray = urlParam.split('&');
      var params = {};
      for(var i=0; i<paramsArray.length; i++) {
        var param = paramsArray[i].split('=');
        if(param.length == 2)
          params[param[0]] = param[1];
      }
      return params;
    }
    return {};
  }
}


mb.debug = {
  log : (typeof console!='undefined' ?
         function(o) { mb.debug.on && console.log(mb.lang.toJSON(o)) } : 
         mb.lang.emptyFunc)
};


/**
 * Broadcaster
 */
var Broadcaster = mb.lang.defineClass({
  _className : 'Broadcaster',
    
  initialize : function() {
    this.events = {};
  }
  ,
  addListener : function(event, func, scope) {
    var listener = { scope : scope, func : func };
    if (this.events[event]) {
      this.events[event].push(listener);
    } 
	else {
      this.events[event] = [ listener ];
    }
  }
  ,
  removeListener : function(event, func) {
    if (this.events[event]) {
      this.events[event] = jQuery.grep(this.events[event], function(listener) {
        return listener.func != func;
      });
    }
  }
  ,
  fire : function(event) {
    var arg = Array.prototype.slice.call(arguments, 1);
    jQuery.each(this.events[event] || [], function(index, listener) {
      listener.func.apply(listener.scope, arg);
    });
  }
}) 


if (typeof DOMParser == "undefined") {
  DOMParser = function () {}

  DOMParser.prototype.parseFromString = function (str, contentType) {
    if (typeof ActiveXObject != "undefined") {
      var d = new ActiveXObject("MSXML.DomDocument");
      d.loadXML(str);
      return d;
    } else if (typeof XMLHttpRequest != "undefined") {
      var req = new XMLHttpRequest;
      req.open("GET", "data:" + (contentType || "application/xml") +
                     ";charset=utf-8," + encodeURIComponent(str), false);
      if (req.overrideMimeType) {
        req.overrideMimeType(contentType);
      }
      req.send(null);
      return req.responseXML;
    }
  }
}

mb.config = jQuery.extend({
  EMPTY_URL : mb.baseURL+'/empty.html',
  SAVE_URL  : mb.baseURL+'/save.html',
  JSONP_PROXY_URL      : mb.baseURL+'/JSONPProxy',
  LIBRARY_SEARCH_URL   : mb.baseURL+'/Libraries',
  PROCESS_OPEN_URL     : mb.baseURL+'/OpenMashup',
  PROCESS_SAVE_URL     : mb.baseURL+'/SaveMashup',
  PROCESS_SAVE_LOC_ULR : mb.baseURL+'/SaveJson',
  PROCESS_PRIVATE_URL  : mb.baseURL+'/LoadMashups',
  PROCESS_REGISTRY_URL : mb.baseURL+'/ShowMashup',
}, mb.config || {});



